package com.lagou.minicat.classloader;


import java.net.URL;
import java.net.URLClassLoader;

/**
 * @ClassName MyClassLoader
 * @Description
 * @Author playboy
 * @Date 2021/5/22 8:06 下午
 * @Version 1.0
 **/
public class MyClassLoader extends URLClassLoader {

	ClassLoader parent;

	public MyClassLoader(URL[] urls, ClassLoader parent) {
		super(urls);
		this.parent = parent;
	}

	@Override
	protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
		synchronized (this.getClassLoadingLock(name)) {
			//先查看是否已经加载过
			Class<?> c = this.findLoadedClass(name);
			if (c == null) {
				long t0 = System.nanoTime();
				//如果没有加载过，现在指定的目录查找
				try {
					c = this.findClass(name);
				} catch (Exception e) {

				}

				if (c == null) {
					long t1 = System.nanoTime();
					//如果依然没有找到，则交给父类去查找
					c = Class.forName(name, false, parent);
				}
			}
			if (resolve) {
				this.resolveClass(c);
			}
			return c;
		}
	}
}
